package cn.edu.uestc.topo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;

public class Node {

    private UUID id = null;
    private String name = null;
    private String city = null;
    private String mgtIP = null;
    private String loopbackIP = null;
    private int status = 1;
    private int inactiveReason = 1;
    private String serialNum = null;
    private String objectOid = null;
    private String softVersion = null;
    private int vendor = 65535;
    private UUID regionId = null;
    private SegmentRoutingCapability srCapability= null;
    private UUID netconfCfgId = null;
    private UUID snmpCfgId = null;
    private List<UUID> portList = null;

    public Node() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMgtIP() {
        return mgtIP;
    }

    public void setMgtIP(String mgtIP) {
        this.mgtIP = mgtIP;
    }

    public String getLoopbackIP() {
        return loopbackIP;
    }

    public void setLoopbackIP(String loopbackIP) {
        this.loopbackIP = loopbackIP;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getInactiveReason() {
        return inactiveReason;
    }

    public void setInactiveReason(int inactiveReason) {
        this.inactiveReason = inactiveReason;
    }

    public String getSerialNum() {
        return serialNum;
    }

    public void setSerialNum(String serialNum) {
        this.serialNum = serialNum;
    }

    public String getObjectOid() {
        return objectOid;
    }

    public void setObjectOid(String objectOid) {
        this.objectOid = objectOid;
    }

    public String getSoftVersion() {
        return softVersion;
    }

    public void setSoftVersion(String softVersion) {
        this.softVersion = softVersion;
    }

    public int getVendor() {
        return vendor;
    }

    public void setVendor(int vendor) {
        this.vendor = vendor;
    }

    public UUID getRegionId() {
        return regionId;
    }

    public void setRegionId(UUID regionId) {
        this.regionId = regionId;
    }

    public List<UUID> getPortList() {
        return portList;
    }

    public void setPortList(List<UUID> portList) {
        this.portList = portList;
    }

    public SegmentRoutingCapability getSrCapability() {
        return srCapability;
    }

    public void setSrCapability(SegmentRoutingCapability srCapability) {
        this.srCapability = srCapability;
    }

    public UUID getNetconfCfgId() {
        return netconfCfgId;
    }

    public void setNetconfCfgId(UUID netconfCfgId) {
        this.netconfCfgId = netconfCfgId;
    }

    public UUID getSnmpCfgId() {
        return snmpCfgId;
    }

    public void setSnmpCfgId(UUID snmpCfgId) {
        this.snmpCfgId = snmpCfgId;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void readFromJson(JSONObject jsonObject) {
        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.name = jsonObject.getString("name");
            this.mgtIP = jsonObject.getString("mgtIP");
            this.loopbackIP = jsonObject.getString("loopbackIP");
            this.status = jsonObject.getInt("status");
            this.inactiveReason = jsonObject.getInt("inactiveReason");
            this.serialNum = jsonObject.getString("serialNum");
            this.objectOid = jsonObject.getString("objectOid");
            this.softVersion = jsonObject.getString("softVersion");
            this.vendor = jsonObject.getInt("vendor");
            this.regionId = UUID.fromString(jsonObject.getString("regionId"));
            this.netconfCfgId = UUID.fromString(jsonObject.getString("netconfCfgId"));
            this.snmpCfgId = UUID.fromString(jsonObject.getString("snmpCfgId"));

            JSONArray portJson = jsonObject.getJSONArray("portList");

            if (this.portList == null)
                this.portList = new ArrayList<>();

            for (int i = 0; i < portJson.length(); i++) {
                UUID portID = UUID.fromString(portJson.getString(i));
                this.portList.add(portID);
            }

            if (this.srCapability == null)
                this.srCapability = new SegmentRoutingCapability();

            this.srCapability.readFromJson(jsonObject.getJSONObject("segmentRoutingCapability"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("name", this.name);
            jsonObject.put("mgtIP", this.mgtIP);
            jsonObject.put("loopbackIP", this.loopbackIP);
            jsonObject.put("status", this.status);
            jsonObject.put("inactiveReason", this.inactiveReason);
            jsonObject.put("serialNum", this.serialNum);
            jsonObject.put("objectOid", this.objectOid);
            jsonObject.put("softVersion", this.softVersion);
            jsonObject.put("vendor", this.vendor);
            jsonObject.put("regionId", this.regionId.toString());
            jsonObject.put("netconfCfgId", this.netconfCfgId.toString());
            jsonObject.put("snmpCfgId", this.snmpCfgId.toString());

            JSONArray portJson = new JSONArray(this.portList);
            jsonObject.put("portList", portJson);

            jsonObject.put("segmentRoutingCapability", this.srCapability.writeToJson());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }


    @Override
    public String toString() {
        String string = null;
        try {
            string = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return string;
    }
}
